import { defineStore } from 'pinia'
import {
  LayoutType,
  PageRegionType,
  WritingMode,
} from '../enums/index'
import {
  PAGE_REGION_COLOR,
  THEME_COLOR,
} from '../settings'
import { timezoneName } from '../utils/src/time'

interface IState {
  timezoneName: string
  themeColor: string
  isGreyMode: boolean
  layoutType: LayoutType
  writingMode: WritingMode
  pageRegionColor: Record<PageRegionType, { background: string, text: string }>
  watermark: boolean
}

export const useConfigStore = defineStore('config-store', {
  state: (): IState => ({
    timezoneName,
    themeColor: THEME_COLOR,
    isGreyMode: false,
    layoutType: LayoutType.ONE,
    writingMode: WritingMode.LTR,
    pageRegionColor: {
      [PageRegionType.LOGO]: {
        background: PAGE_REGION_COLOR.LOGO.background,
        text: PAGE_REGION_COLOR.LOGO.text,
      },
      [PageRegionType.HEADER]: {
        background: PAGE_REGION_COLOR.HEADER.background,
        text: PAGE_REGION_COLOR.HEADER.text,
      },
      [PageRegionType.TAB]: {
        background: PAGE_REGION_COLOR.TAB.background,
        text: PAGE_REGION_COLOR.TAB.text,
      },
      [PageRegionType.ASIDE]: {
        background: PAGE_REGION_COLOR.ASIDE.background,
        text: PAGE_REGION_COLOR.ASIDE.text,
      },
      [PageRegionType.MAIN]: {
        background: PAGE_REGION_COLOR.MAIN.background,
        text: PAGE_REGION_COLOR.MAIN.text,
      },
    },
    watermark: true,
  }),

  getters: {
    getThemeColor: state => state.themeColor,

    getIsGreyMode: state => state.isGreyMode,

    getLayoutType: state => state.layoutType,

    getWritingMode: state => state.writingMode,

    getPageRegionColor: state => state.pageRegionColor,
  },

  actions: {
    setTimezoneName(name: string) {
      this.timezoneName = name
    },

    setThemeColor(color: string) {
      this.themeColor = color
    },

    setGreyMode(mode: boolean) {
      this.isGreyMode = mode
    },

    setLayoutType(type: LayoutType) {
      this.layoutType = type
    },

    setWritingMode(mode: WritingMode) {
      this.writingMode = mode
    },

    setPageRegionColor(region: PageRegionType, field: 'background' | 'text', value: string) {
      this.pageRegionColor[region][field] = value
    },

    setWatermark(status: boolean) {
      this.watermark = status
    },
  },

  persist: true,
})

export default useConfigStore
